package com.cc.app.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

public class Demo extends TestView {

    private boolean paused = false;

    private int duration = 1000;

    private float ratio;

    private Paint paint;

    private ValueAnimator animator;

    public Demo(Context context) {
        this(context, null);
    }

    public Demo(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        initPaint();
        initAnimator();
    }

    private void initAnimator() {
        animator = ValueAnimator.ofFloat(0, 1).setDuration(duration);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ratio = (float) animation.getAnimatedValue();
                invalidate();
            }
        });

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (paused)
                    return;
            }
        });
    }

    @Override
    public void action1() {
        paused = false;
        animator.start();
    }

    public void action2() {
        paused = true;
        animator.end();
    }

    private void initPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.BEVEL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.translate(getWidth() / 2, getHeight() / 2);

        paint(canvas);
        canvas.restore();
    }

    float[] points = new float[2];

    private void paint(Canvas canvas) {
//        canvas.rotate(360 * ratio);
        Matrix matrix = new Matrix();
        float scaleX = 1;
        float scaleY = 1;
        matrix.postScale(scaleX, scaleY);
        float transX = -5;
        float transY = -5;
        matrix.postTranslate(transX, transY);
        int angle = (int) (ratio * 360);
        matrix.postRotate(angle, transX, transY);

        matrix.mapPoints(points);

        canvas.translate(points[0], points[1]);
        draw3(canvas, matrix);
    }


    /**
     * 五角星
     *
     * @param canvas
     */
    private void draw2(Canvas canvas) {
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);

        int r = 50;
        Path path = new Path();
        path.addArc(new RectF(-r, -r, r, r), -90, -359.9f);

        canvas.drawPath(path, paint);

        PathMeasure m = new PathMeasure();
        m.setPath(path, false);

        Path path2 = new Path();
        path2.moveTo(0, -r);

        float[] pos = new float[2];
        canvas.save();

        int p = 0;
        for (int i = 0; i < 5; i++) {
            p = (p + 2) % 5;
            m.getPosTan(m.getLength() * p / 5, pos, null);
            path2.lineTo(pos[0], pos[1]);
        }

        canvas.drawPath(path2, paint);

        canvas.restore();
    }

    /**
     * 太极
     *
     * @param canvas
     */
    private void draw3(Canvas canvas, Matrix matrix) {

        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        int r = 80;
        canvas.drawArc(new RectF(-r, -r, r, r), -90, 180, true, paint);

        paint.setColor(Color.BLACK);
        canvas.drawArc(new RectF(-r, -r, r, r), 90, 180, true, paint);

        canvas.drawCircle(0, -r / 2, r / 2, paint);
        paint.setColor(Color.WHITE);

        canvas.drawCircle(0, r / 2, r / 2, paint);
        canvas.drawCircle(0, -r / 2, r / 8, paint);

        paint.setColor(Color.BLACK);
        canvas.drawCircle(0, r / 2, r / 8, paint);
    }
}